﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Productions;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;
using NPOI.SS.Formula.Functions;
using NPOI.Util;

namespace MangoMes.Write.API.Applications.ArrivalNotice
{
    public class NoticelinesCreateHandler : IRequestHandler<NoticelinesCreateCommand, ResultsDTO>
    {
        #region 仓储 
        private readonly IBateRepository<Noticelines> NoticelinesRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="NoticelinesRepository"></param>
        public NoticelinesCreateHandler(IBateRepository<Noticelines> NoticelinesRepository)
        {
            this.NoticelinesRepository = NoticelinesRepository;
        }
        #endregion

        #region 到货通知单行 新增
        /// <summary>
        /// 到货通知单行 新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultsDTO> Handle(NoticelinesCreateCommand request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                //初始化一个ResultDTO对象result，并设置Code为 0
                ResultsDTO result = new ResultsDTO() { Code = 0 };
                DapperHelper helper = new DapperHelper(); //创建DapperHelper对象 用于与数据库进行交互

                Noticelines noticelines = new Noticelines()
                {
                    NoticeLineId = request.NoticeLineId,
                    productManageNode = request.productManageNode,
                    productManageName = request.productManageName,
                    unites = request.unites,
                    productManageNumber = request.productManageNumber,
                    IsNos = request.IsNos,
                    Remarks = request.Remarks,
                    NotStates = request.NotStates,
                    NoticeCount = request.NoticeCount,
                    ArrivalNoticeNode = request.ArrivalNoticeNode,
                };

                //连接mySql 
                string conn = "server=112.124.69.237;port=3306;uid=root;pwd=No6123666;database=MangoMesDB";

                //sql语句
                string str = $"insert into noticelines values(@NoticeLineId,@productManageNode,@productManageName,@unites,@productManageNumber,@IsNos,@Remarks,@NotStates,@NoticeCount,@ArrivalNoticeNode)";

                result.Code = await NoticelinesRepository.Add(str, noticelines, conn);

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
